package cn.hjh.service;

import cn.hjh.dao.DistrictDao;
import cn.hjh.dao.HibernateUtil;
import cn.hjh.pojo.District;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import sun.security.ssl.HandshakeInStream;

public class DistrictBiz {
    private DistrictDao districtDao = new DistrictDao();

    public void addNewDistrict(District district){
        Transaction tx = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            districtDao.save(district);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    public void updateDistrict(District district){
        Transaction tx = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            //通过get（）或load（）方法加载要修改的对象
            District districtToUpdate = districtDao.get(district.getId());
            districtToUpdate.setName(district.getName());
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }
    public District findDistrictById(Integer id) {
        return this.districtDao.selectDistrictById(id);
    }
}
